﻿using B.S.Data.MES.API.Applications.Management.SteamApplication.Command.FactoryCommand;
using B.S.Domain.Accounts;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;

namespace B.S.Data.MES.API.Applications.Management.SteamApplication.CommandHandler.FactoryCommandHandler
{
    public class FactoryDelCommandHandler : IRequestHandler<FactoryDelCommand, APIRequst<int>>
    {
        public FactoryDelCommandHandler(IBaseRepository<FactoryClass> factoryRepository)
        {
            FactoryRepository = factoryRepository;
        }

        public IBaseRepository<FactoryClass> FactoryRepository { get; }

        public async Task<APIRequst<int>> Handle(FactoryDelCommand request, CancellationToken cancellationToken)
        {
            var path = FactoryRepository.GetAll().FirstOrDefault(a => a.FactoryId == request.Id);
            if (path == null)
            {
                return Task.FromResult(new APIRequst<int> { Code=RequstCode.失败,Msg="工厂信息不存在",Data=0}).Result;
            }
            else
            {
                path.isDel = true;
                path.DeleteName = request.Name;
                path.DeleteTime = DateTime.Now;
                return Task.FromResult(new APIRequst<int> { Code = RequstCode.成功, Msg = "工厂信息删除成功", Data = await FactoryRepository.Update(path) }).Result;
            }
        }
    }
}
